﻿using System;
using System.Data;
using System.Collections.Generic;

using Clipper.Model;
using System.Collections.ObjectModel;
namespace Clipper.BLL
{
	/// <summary>
	/// DetectResult 查询和显示项
	/// </summary>
	public partial class DetectResult
	{
		private readonly Clipper.DAL.DetectResult dal=new Clipper.DAL.DetectResult();
        private SampleInfo mSampleBll = new SampleInfo();//样本访问
		public DetectResult()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int reactionCupID)
		{
			return dal.Exists(reactionCupID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Clipper.Model.DetectResult model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Clipper.Model.DetectResult model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int reactionCupID)
		{
			
			return dal.Delete(reactionCupID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string reactionCupIDlist )
		{
			return dal.DeleteList(reactionCupIDlist );
		}
        public DataSet GetStardList()
        {
            return dal.GetStardList();
        }
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.DetectResult GetModel(int reactionCupID)
		{
			
			return dal.GetModel(reactionCupID);
		}

		

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public ObservableCollection<Clipper.Model.DetectResult> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public ObservableCollection<Clipper.Model.DetectResult> DataTableToList(DataTable dt)
		{
			ObservableCollection<Clipper.Model.DetectResult> modelList = new ObservableCollection<Clipper.Model.DetectResult>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Clipper.Model.DetectResult model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
                        model.SampleInfo = mSampleBll.GetModel(model.SampleID);
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod
		#region  ExtensionMethod

        /// <summary>
        /// 获得查询结果数据
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<Model.DetectResult> GetDetectResult()
        {
            ObservableCollection<Model.DetectResult> detectResult = new ObservableCollection<Model.DetectResult>();

            return detectResult;
        }

        #region 查询报告数据
        /// <summary>
        /// 获得检测数据
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="sampleId"></param>
        /// <param name="detectItem"></param>
        /// <param name="exception"></param>
        /// <param name="detecting"></param>
        /// <param name="detected"></param>
        /// <returns></returns>
        public ObservableCollection<Model.DetectResult> GetDetectResult
            (string start,string end,int? sampleStart,int? sampleEnd,string detectItem,bool exception)
        {
            string strWhere = "";
            if (start != null && end != null)
            {
                strWhere += " checkDatetime>='" + start + "' and checkDatetime<='" + end + "'";
            }
            if (sampleStart != null&&sampleEnd!=null)
            {
                strWhere += "and SampleID>=" + sampleStart + " and SampleID<=" + sampleEnd;
            }
            if (detectItem != "")
            {
                strWhere += "and DetectItem='"+detectItem+"'";
            }
            if (exception)
            {
                strWhere += "and ResultState=1";
            }
            else
            {

            }
            if (strWhere.IndexOf("and") == 0)
            {
               strWhere= strWhere.Substring(3);
            }
            ObservableCollection<Model.DetectResult> modelList= GetModelList(strWhere);
            return modelList;
        }

        /// <summary>
        /// 查询打印报告数据
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<Model.DetectResult> GetDetectReport
             (DateTime? start, DateTime? end, int? sampleStart, int? sampleEnd, string name, 
                int? patientStart,int? patientEnd)
        {
            string strWhere = "";
            if (start != null && end != null)
            {
                strWhere += " checkDatetime>='" + start + "' and checkDatetime<='" + end + "'";
            }
            if (sampleStart != null && sampleEnd != null)
            {
                strWhere += "and SampleID>=" + sampleStart + " and SampleID<=" + sampleEnd;
            }
            if (name != "")
            {
                strWhere += "and DetectItem='" + name + "'";
            }
            //if (patientStart != null && patientEnd != null)
            //{
            //    strWhere +=
            //}
            if (strWhere.IndexOf("and") == 0)
            {
                strWhere=strWhere.Substring(3);
            }
            ObservableCollection<Model.DetectResult> modelList = GetModelList(strWhere);
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public ObservableCollection<Clipper.Model.DetectResult> DataTableToReportList(DataTable dt)
        {
            ObservableCollection<Clipper.Model.DetectResult> modelList = new ObservableCollection<Clipper.Model.DetectResult>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Clipper.Model.DetectResult model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        model.SampleInfo = mSampleBll.GetFullSampleInfo(model.SampleID);
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
        #endregion

        #endregion  ExtensionMethod
    }
}

